package com.vote.controller;

import java.sql.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.vote.entity.Channel;
import com.vote.entity.Ip;
import com.vote.entity.Vote;
import com.vote.entity.VoteOption;
import com.vote.service.ChannelService;

@Controller
public class ChannerController {

	@Autowired
	private ChannelService channelService;

	@GetMapping("channels")
	public ModelAndView channels(String msg) {
		ModelAndView modelAndView = new ModelAndView("channels");
		List<Channel> channels = channelService.getAllChannel();
		modelAndView.addObject("channels", channels);
		modelAndView.addObject("msg", msg);
		return modelAndView;

	}

	@GetMapping("votes/{channelId}")
	public ModelAndView voteList(@PathVariable("channelId") int channelId) {
		ModelAndView modelAndView = new ModelAndView("vote_index");
		// 根据频道id拿到该频道的所有投票信息以及它的选项
		List<Vote> voteList = channelService.getVoteList(channelId);
		modelAndView.addObject("voteList", voteList);
		return modelAndView;
	}
	
	
	@GetMapping("vote/{channelId}")
	public ModelAndView voteLists(@PathVariable("channelId") int channelId) {
		ModelAndView modelAndView = new ModelAndView("vote_result");
		// 根据频道id拿到该频道的所有投票信息以及它的选项
		List<Vote> voteList = channelService.getVoteList(channelId);
		modelAndView.addObject("voteList", voteList);
		return modelAndView;
	}

	@GetMapping("voteResult/{channelId}")
	public ModelAndView voteResult(@PathVariable("channelId") int channelId, HttpServletRequest request) {
		
		ModelAndView modelAndView = new ModelAndView();
		
		String[] oid = request.getParameter("optionid").split(",");
		
		//获取客户端ip
		String ip = request.getRemoteAddr();
		
		//new 一个当前时间
		Date date = new Date(System.currentTimeMillis());

		//查看数据库ip表里是否有同个ip同一天里给某个频道投票
		//如果有,返回频道列表页面
		//如果没有,进行投票操作,投票成功之后向ip表里添加一条新的数据
		Ip ipBean = channelService.getIP(ip, date, channelId);

		int result=0;
		if (ipBean == null) {
			for (int i = 0; i < oid.length; i++) {
				int num = Integer.parseInt(oid[i]);
				// 根据传来的optionId 拿到一条信息
				VoteOption voteOption = channelService.getvoteOption(num);
				// 修改投票选项表里的票数值 +1
				result = channelService.updateVoteOption(voteOption);
			}
		} else {
			//如果新的时间大于旧的时间超过一天，可以再次投票
			long d = date.getTime() - ipBean.getTime().getTime();
			if (d > (24 * 3600000)) {
				for (int i = 0; i < oid.length; i++) {
					int num = Integer.parseInt(oid[i]);
					// 根据传来的optionId 拿到一条信息
					VoteOption voteOption = channelService.getvoteOption(num);
					// 修改投票选项表里的票数值 +1
					result = channelService.updateVoteOption(voteOption);
				
				}
			} else {
				modelAndView=new ModelAndView("redirect:/channels");
			}
		}
		if (result>0) {
			Ip ipBean2 = new Ip();
			ipBean2.setChannelId(channelId);
			ipBean2.setIp(ip);
			ipBean2.setTime(date);
			int addip = channelService.addIp(ipBean2);
			modelAndView=new ModelAndView("redirect:/vote/"+channelId);
		}

		return modelAndView;

	}

	@GetMapping("addOption/{voteid}/{addvalue}/{maxUserOption}")
	@ResponseBody
	public Object addOption(@PathVariable("voteid") int voteId, @PathVariable("addvalue") String addvalue,
			@PathVariable("maxUserOption1") int maxUserOption) {

		// 根据投票标题id拿到它的最大选项，也就是这个投票现有的选项长度
		int maxSqeNumber = channelService.getMaxSqeNumber(voteId);

		Map<String, Object> result = new HashMap<String, Object>();

		int add = 0;
		// 判断传来投票的最大选项长度是否大于该投票的现有选项长度
		// 大于则可以添加新选项
		if (maxUserOption > maxSqeNumber) {
			// 根据 投票id 和 新填写的选项 查找一条信息
			// 如果有 选项重复 不可以添加 如果==null则添加一个选项
			VoteOption voteOption = channelService.getAddVoteOption(addvalue, voteId);
			if (voteOption == null) {
				VoteOption option = new VoteOption();
				option.setByUser(1);
				option.setTicketNumber(0);
				option.setVoteId(voteId);
				option.setTitle(addvalue);
				option.setSeqNumber(maxSqeNumber);
				add = channelService.addOption(option);
				if (add > 0) {
					result.put("flag", "success");
				}
			} else {
				result.put("flag", "fail2");
			}
		} else {
			result.put("flag", "fail");
		}

		return result;

	}
}
